package com.yxuan.springboot.controller;

import com.yxuan.springboot.model.TestOptions;
import com.yxuan.springboot.model.TestQuestions;
import com.yxuan.springboot.model.TestTypeDetails;
import com.yxuan.springboot.service.TestOptionsService;
import com.yxuan.springboot.service.TestQuestionsService;
import com.yxuan.springboot.service.TestTypeDetailsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zchao
 * TestQuestions控制器
 * @date 2023-05-16 14:22:57
 */
@Controller
@Slf4j
@Api(tags = "")
@RequestMapping(value = "/testquestions", method = {RequestMethod.POST, RequestMethod.GET})
public class TestQuestionsController extends BaseController<TestQuestions> {

    @Autowired
    private TestQuestionsService testQuestionsService;

    @Autowired
    private TestTypeDetailsService testTypeDetailsService;

    @Autowired
    private TestOptionsService testOptionsService;

//     新增题目，同时新增选项
    @RequestMapping(value = "/addquestion", method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> addQuestion(@RequestBody Map map) throws Exception {
        try{
            return testQuestionsService.addQuestion(map);
        }catch (Exception e){
            HashMap<String,Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result",ERROR);
            return result;
        }
    }



//    修改题目


    @RequestMapping(value = "/updatequestion", method = RequestMethod.POST)
    @ResponseBody
    public HashMap<String,Object> updTopic(@RequestBody TestQuestions entity){
        try{
            HashMap<String,Object> result = new HashMap<>();
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            entity.setUpdateTime(timestamp);
            testQuestionsService.upd(entity);
            result.put("upd_topic","success");
            return result;
        }catch (Exception e){
            HashMap<String,Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result",ERROR);
            return result;
        }
    }


    //    查询所有题目
    @RequestMapping("/alltopic")
    @ResponseBody
    public HashMap<String, Object> allTopic(@RequestBody Map map) throws Exception {
        try {
            int page = (int) map.get("page");
            int limit = (int) map.get("limit");
            TestQuestions testQuestions = new TestQuestions();
            HashMap<String, Object> listByPramas = (HashMap<String, Object>) testQuestionsService.findListByPramas(page, limit, testQuestions);
            return listByPramas;
        } catch (Exception e) {
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }

    }

    //   根据类别名字查询题目
    @RequestMapping("/topicbyname")
    @ResponseBody
    public HashMap<String, Object> findTopicByName(@RequestBody Map map) throws Exception {
        try{
            int page = (int) map.get("page");
            int limit = (int) map.get("limit");
            TestQuestions testQuestions = new TestQuestions();
            TestTypeDetails testTypeDetails = new TestTypeDetails();
            testTypeDetails.setTypeName((String) map.get("name"));
            TestTypeDetails objectByPramas = testTypeDetailsService.findObjectByPramas(testTypeDetails);
            testQuestions.setTypeId(objectByPramas.getId());
            HashMap<String, Object> listByPramas = (HashMap<String, Object>) testQuestionsService.findListByPramas(page, limit, testQuestions);
            return listByPramas;
        }catch (Exception e){
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }
    }



//    根据 题目id 查找所有选项
    @RequestMapping("/allanswer")
    @ResponseBody
    public HashMap<String, Object> allAnswer(@RequestBody Map map){
        try{
            HashMap<String, Object> result = new HashMap<>();
            TestOptions options = new TestOptions();
            options.setQuestionId((Integer) map.get("id"));
            List<TestOptions> manyByPramas = testOptionsService.findManyByPramas(options);
            result.put("options",manyByPramas);
            return result;
        }catch (Exception e){
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }
    }
}
